home *** CD-ROM | disk | FTP | other *** search
- /*
- * Blob Manager Demonstration: Hebrew Alephbet module
- *
- * 26 July 1986 Paul DuBois
- *
- * 28 Dec 93
- * - Convert blobs from picture blobs to procedure blobs.
- * This gives better donor dimming on monitors that can do grayscale
- * and drawing turns out to be a lot faster, too.
- * - Receptor dimming is no longer done by inverting the blob, since
- * that was done by changing the dimming pattern and mode, which don't
- * as yet apply to procedure blobs. I just gray them per the default
- * dimming. (Note: the code to change the dimming pattern/mode is
- * still here as a reminder of when it needs to be changed, should I
- * figure out how to make it apply to procedure blobs.)
- */
-
- # include "TransSkel.h"
-
-
- # include "BlobMgr.h"
- # include "BlobDemo.h"
-
-
- # define nLetters 22 /* 22 letters in Hebrew alephbet */
- # define sitmSize 16 /* sicn item is 16 x 16 */
- # define rWidth 76 /* receptor blob width and height */
- # define rHeight 22
- # define rHGap 2 /* horizontal and vertical gaps */
- # define rVGap 5 /* between receptor blobs */
- # define dWidth sitmSize /* ditto for donors */
- # define dHeight sitmSize
- # define dHGap 12
- # define dVGap 4
-
- # define pauseAns 1 /* what kind of pause? */
- # define pauseMatch 2
-
- typedef short Sitm[sitmSize];
- typedef Sitm Sicn[1];
- typedef Sicn **SicnHandle;
-
- static WindowPtr wind;
- static FontInfo fontInfo;
- static BlobSetHandle hebDonors;
- static BlobSetHandle hebReceptors;
- static SicnHandle letterSicn;
-
- static ControlHandle checkCtl;
- static ControlHandle resumeCtl;
- static ControlHandle resetCtl;
- static ControlHandle shuffleCtl;
- static ControlHandle ansCtl;
-
- static Boolean paused = false;
- static short pauseType;
-
-
- /*
- * Plot sicn item in given rectangle.
- * This is exactly analogous to the ToolBox PlotIcon routine,
- * except that the item number within the sicn must be specified.
- */
-
-
- static void
- PlotSicn (Rect *r, SicnHandle s, short i)
- {
- GrafPtr thePort;
- BitMap bm;
- Sitm tmpSitm;
-
- /* create a small bitmap */
- HLock ((Handle) s);
- BlockMove (&((**s)[i]), &tmpSitm, (long) sizeof (Sitm));
- HUnlock ((Handle) s);
- bm.baseAddr = (Ptr) &tmpSitm;
- bm.rowBytes = 2; /* items are 16 bits wide */
- SetRect (&bm.bounds, 0, 0, sitmSize, sitmSize);
- GetPort (&thePort);
- CopyBits (&bm, &thePort->portBits, &bm.bounds, r, srcCopy, nil);
- }
-
-
- static void
- LightControls (void)
- {
- short onIfPaused, offIfPaused;
-
- onIfPaused = (paused ? normalHilite : dimHilite);
- offIfPaused = (paused ? dimHilite : normalHilite);
- HiliteControl (checkCtl, offIfPaused);
- HiliteControl (resetCtl, offIfPaused);
- HiliteControl (ansCtl, offIfPaused);
- HiliteControl (resumeCtl, onIfPaused);
- }
-
-
- /*
- * Draw a donor blob (one of the Hebrew letters, the index of which
- * is specified as the blob's reference constant). Since these have
- * an empty static region, partCode is always inDragBlob and can be
- * ignored.
- */
-
- static pascal void
- DrawDonor (BlobHandle bDst, BlobHandle bSrc, short partCode)
- {
- Rect r;
-
- r = BDragBox (bDst); /* draw it in the destination blob */
- PlotSicn (&r, letterSicn, GetBRefCon (bSrc));
- }
-
-
- /*
- * Draw a receptor blob. The reference constant low word is the index of
- * the title string. These are never glued to another blob, so bSrc and
- * bDst are always the same.
- */
-
- static pascal void
- DrawReceptor (BlobHandle bDst, BlobHandle bSrc, short partCode)
- {
- Rect r;
- Str255 str;
- short h, v;
-
- if (partCode == inDragBlob) /* draw drag region */
- {
- r = BDragBox (bDst);
- EraseRect (&r);
- FrameRect (&r);
- }
- else /* draw static region */
- {
- r = BStatBox (bDst);
- GetIndString (str, nameStrNo, LoWord (GetBRefCon (bSrc)));
- h = (r.left + r.right - StringWidth (str)) / 2;
- v = r.bottom - fontInfo.descent;
- MoveTo (h, v);
- DrawString (str);
- }
- }
-
-
- static void
- MoveBlobs (BlobSetHandle bSet, short n1, short n2,
- short x, short y, short xd)
- {
- for ( ; n1 <= n2; ++n1)
- {
- MoveBlob (GetBlobHandle (bSet, n1), inFullBlob, x, y);
- x += xd;
- }
- }
-
-
- static void
- MakeBlobs (void)
- {
- short i;
- short h, v;
- Rect tRect, sitmRect;
- BlobHandle b1, b2;
- short hMid;
- short x, y, delta;
-
- hMid = wind->portRect.right / 2 - 4;
-
- letterSicn = (SicnHandle) GetResource ('SICN', letrSicnNo);
- DetachResource ((Handle) letterSicn);
- hebDonors = NewBlobSet ();
- hebReceptors = NewBlobSet ();
- for (i = 0; i < nLetters; ++i)
- {
- b1 = NewBlob (hebDonors, true, 1, false, (long) i);
- b2 = NewBlob (hebReceptors, true, 0, true, (long) (i + 1));
-
- SetRect (&sitmRect, 0, 0, sitmSize, sitmSize);
- SetProcRectBlob (b1, DrawDonor, &sitmRect, &sitmRect);
-
- OffsetRect (&sitmRect, rWidth-sitmSize, 5);
- SetRect (&tRect, 0, 5, rWidth-sitmSize-1, rHeight);
- SetProcRectBlob (b2, DrawReceptor, &sitmRect, &tRect);
-
- NewBlobMatch (b1, b2); /* assign donor -> receptor mapping */
- }
-
- /*
- * now arrange the blobs on the board.
- */
- x = hMid - 5*(dWidth+dHGap) - dWidth/2;
- delta = dWidth + dHGap;
- y = 5;
- MoveBlobs (hebDonors, 0, 10, x, y, delta);
- y += dHeight + dVGap;
- MoveBlobs (hebDonors, 11, 21, x, y, delta);
-
- x = hMid - 3*(rWidth+rHGap);
- y += dHeight + dVGap + 5;
- delta = rWidth + rHGap;
- MoveBlobs (hebReceptors, 0, 5, x, y, delta);
- y += rHeight + rVGap;
- MoveBlobs (hebReceptors, 6, 11, x, y, delta);
- y += rHeight + rVGap;
- MoveBlobs (hebReceptors, 12, 17, x, y, delta);
- x = hMid - 2*(rWidth+rHGap);
- y += rHeight + rVGap;
- MoveBlobs (hebReceptors, 18, 21, x, y, delta);
-
- ShuffleBlobSet (hebDonors);
- }
-
-
- /*
- * Handle mouse click. When Answer is clicked, stick the index of the
- * current glob in the high word of the receptor reference constant so
- * it can be restored when Resume is clicked.
- */
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- ControlHandle ctl;
- BlobHandle b, g;
- short mode;
- Pattern p;
- short index;
- long refCon;
-
- if (!paused)
- {
- BlobClick (pt, t, hebDonors, hebReceptors);
- if (BlobSetQuiet (hebReceptors)) /* if all correct */
- { /* override normal */
- paused = true; /* control display */
- pauseType = pauseAns;
- LightControls ();
- HiliteControl (resumeCtl, dimHilite);
- HiliteControl (resetCtl, normalHilite);
- FreezeBlobSet (hebReceptors);
- }
- }
- if (FindControl (pt, wind, &ctl))
- {
- if (TrackControl (ctl, pt, nil))
- {
- if (ctl == checkCtl)
- {
- paused = true;
- pauseType = pauseMatch;
- GetBDimInfo (&p, &mode); /* hilite by inverting */
- SetBDimInfo (black, patXor);
- BlobFeedback (hebReceptors, normalDraw, dimDraw);
- SetBDimInfo (p, mode);
- LightControls ();
- if (BlobSetQuiet (hebReceptors)) /* if all correct */
- { /* override normal */
- HiliteControl (resumeCtl, dimHilite); /* control display */
- HiliteControl (resetCtl, normalHilite);
- }
- }
- else if (ctl == ansCtl)
- {
- paused = true;
- pauseType = pauseAns;
- LightControls ();
- for (b = FirstBlob (hebReceptors); b != nil; b = NextBlob (b))
- {
- if (BGlob (b) == (BlobHandle) nil)
- index = 0xffff;
- else
- index = GetBlobIndex (hebDonors, BGlob (b));
- refCon = LoWord (GetBRefCon (b)) | ((long) index << 16);
- SetBRefCon (b, refCon); /* save current glob setting */
- g = FirstBMatch (b); /* attach real answer */
- if (g != BGlob (b))
- ZGlueGlob (g, b);
- }
- }
- else if (ctl == resumeCtl)
- {
- paused = false;
- LightControls ();
- if (pauseType == pauseMatch)
- ThawBlobSet (hebReceptors);
- else
- {
- /* restore previous attachments */
- for (b = FirstBlob (hebReceptors); b != nil; b = NextBlob (b))
- {
- index = HiWord (GetBRefCon (b));
- SetBRefCon (b, LoWord (GetBRefCon (b)));
- if (index == 0xffff)
- g = (BlobHandle) nil;
- else
- g = GetBlobHandle (hebDonors, index);
- if (g != BGlob (b))
- {
- ZUnglueGlob (b);
- GlueGlob (g, b);
- }
- }
- }
- }
- else if (ctl == resetCtl)
- {
- paused = false;
- LightControls ();
- ThawBlobSet (hebReceptors);
- ZUnglueGlobSet (hebReceptors); /* clear and redraw */
- }
- else if (ctl == shuffleCtl)
- {
- ShuffleBlobSet (hebDonors); /* shuffle letters */
- ValidRect (&wind->portRect);
- }
- }
- }
- }
-
-
- static pascal void
- Update (Boolean resized)
- {
- short mode;
- Pattern p;
-
- DrawControls (wind);
- DrawBlobSet (hebDonors);
- GetBDimInfo (&p, &mode);
- if (paused && pauseType == pauseMatch)
- SetBDimInfo (black, patXor);
- DrawBlobSet (hebReceptors);
- SetBDimInfo (p, mode);
- }
-
-
- static pascal void
- Activate (Boolean active)
- {
- if (active)
- {
- SetDragRects (wind);
- SetBCPermissions (true, true, false, true, true);
- }
- }
-
-
- static pascal void
- Clobber (void)
- {
- DisposeHandle ((Handle) letterSicn);
- DoWClobber ();
- }
-
-
- void
- HebInit (void)
- {
- Rect r;
- short i, j;
-
- SkelWindow (wind = GetDemoWind (hebWindRes),
- Mouse, /* mouse clicks */
- nil, /* key clicks */
- Update, /* updates */
- Activate, /* activate/deactivate events */
- nil, /* close window */
- Clobber, /* dispose of window */
- nil, /* idle proc */
- false); /* irrelevant, since no idle proc */
-
- GetFontInfo (&fontInfo);
-
- MakeBlobs ();
- i = wind->portRect.bottom - 25;
- j = wind->portRect.right/2 - 205;
- SetRect (&r, j, i, j+70, i+20);
- checkCtl =
- NewControl (wind, &r, "\pCheck", true, 0, 0, 0, pushButProc, 0L);
- OffsetRect (&r, 85, 0);
- resumeCtl =
- NewControl (wind, &r, "\pResume", true, 0, 0, 0, pushButProc, 0L);
- OffsetRect (&r, 85, 0);
- ansCtl =
- NewControl (wind, &r, "\pAnswer", true, 0, 0, 0, pushButProc, 0L);
- OffsetRect (&r, 85, 0);
- resetCtl =
- NewControl (wind, &r, "\pReset", true, 0, 0, 0, pushButProc, 0L);
- OffsetRect (&r, 85, 0);
- shuffleCtl =
- NewControl (wind, &r, "\pShuffle", true, 0, 0, 0, pushButProc, 0L);
-
- LightControls ();
-
- MakeFrontWind (wind);
- }
-
-